﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(CompraMetaData))]
    public partial class Compra {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public IEnumerable<Compra> Listar()
        {
            var result = from compra in context.Compras
                         select compra;
            return result;
        }

        public Compra Buscar(int iCodigo)
        {
            var result = from compra in context.Compras
                         where compra.comCodigo == iCodigo
                         select compra;
            return result.Count() > 0 ? result.First() : new Compra();
        }

        public IEnumerable<Compra> Consultar(int? iCodigo, int? iProveedor, int? iEmpleado,DateTime? dtFecha)
        {
            var query = from compra in context.Compras
                        select compra;
            if (iCodigo == null && iProveedor == null&& iEmpleado == null&& dtFecha==null)
            {
                return new List<Compra>();
            }
            else
            {
                if (iCodigo != null)
                {
                    query = query.Where(w => w.comCodigo == iCodigo);
                }
                if (iProveedor != null)
                {
                    query = query.Where(w => w.comProveedor == iProveedor);
                }
                if (iEmpleado != null)
                {
                    query = query.Where(w => w.comEmpleado == iEmpleado);
                }
                if (dtFecha != null)
                {
                    query = query.Where(w => w.comFecha == dtFecha);
                }
                return query;
            }

        }
        public bool Guardar(Compra compra, List<DetalleCompra> detallecompra) {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Compra>(compra);
                    repositorio.Save();
                    foreach (var detalle in detallecompra) {
                        detalle.detcomCompra = compra.comCodigo;
                        repositorio.Insert<DetalleCompra>(detalle);
                        repositorio.Save();
                    }  
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Guardar(Compra compra)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Compra>(compra);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(Compra compra)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<Compra>(compra);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(Compra compra)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<Compra>(compra);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class CompraMetaData
    {
        [DisplayName("Codigo")]
        public int comCodigo { get; set; }
        [DisplayName("Total Compra")]
        public decimal comTotal { get; set; }
        [DisplayName("Fecha")]
        public System.DateTime comFecha { get; set; }
        [DisplayName("Codigo Proveedor")]
        public int comProveedor { get; set; }
        [DisplayName("Codigo Empleado")]
        public int comEmpleado { get; set; }
    }
}
